#####################################################
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# FileName: my_monitor.py
# Description: 每五秒向influx数据库写入cpu,memory,disk,network占用率
# Version: 1.0
# Author: 管一鸣
# Date: 2022-05-15
# Platform: ubuntu
#####################################################
import sys
import time
from influxdb import InfluxDBClient

server_ip = sys.argv[1]
virtual_ip = sys.argv[2]
username = sys.argv[3]
pwd = sys.argv[4]
client = InfluxDBClient('localhost', 8086, 'root', '123456')
# get database
database_list = client.get_list_database()
flag = False
for elem in database_list:
    if elem['name'] == 'v_and_s':
        flag = True
        break

time.sleep(2)
if flag:
    client.switch_database('v_and_s')
else:
    client.create_database('v_and_s')
    client.switch_database('v_and_s')

text = [
        {
            "measurement": "ip_info",
            "fields": {
                'server_ip': server_ip,
                'virtual_ip': virtual_ip,
                'username': username,
                'pwd': pwd
            }
        }
    ]
# print(text)
client.write_points(text)